//% color="#24C48E"
namespace Blynk {
    //% block="准备USB连接Blynk 授权密钥[AUTH]"  blockType="command"
    //% AUTH.shadow="string" AUTH.defl="********************************"
    export function WifiConnect(parameter:any){
        let auth=parameter.AUTH.code;

        Generator.addInclude("BlynkHead1",`#include <SoftwareSerial.h>`);
        Generator.addInclude("BlynkHead2",`#include <BlynkSimpleStream.h>`);
        Generator.addSetup("BlynkReady1",`SoftwareSerial SwSerial(10, 11); // RX, TX`)
        Generator.addSetup("BlynkReady2",`SwSerial.begin(9600);`);
        Generator.addSetup("BlynkReady3",`Serial.begin(9600);`);
        Generator.addSetup("BlynkUSBConnect",`Blynk.begin(Serial, ${auth});`);

    }

    //% block="当连接 Blynk 时"  blockType="hat""
    export function WhenConnect(){

        Generator.addEvent("BlynkConnect", "BLYNK_CONNECTED", "", "", true);

    }

    //% block="运行 Blynk 主进程"  blockType="command"
    export function RunMain(){

        Generator.addCode("Blynk.run();");

    }

    //% block="---"
    export function noteSep0() {}

    //% block="当虚拟引脚 V[NUM] 收到数据时"  blockType="hat"
    //% NUM.shadow="number" NUM.defl="0"
    export function whenVirutalPin(parameter:any){
        let pin=parameter.NUM.code;

        Generator.addEvent("BlynkWrite", "BLYNK_WRITE", "", `V${pin}`, true);

    }

    //% block="将数据转换为 int"  blockType="reporter"
    export function paramAsInt(){

        Generator.addCode("param.asInt()");

    }

    //% block="将数据转换为 float"  blockType="reporter"
    export function paramAsFloat(){

        Generator.addCode("param.asFloat()");

    }

    //% block="将数据转换为 double"  blockType="reporter"
    export function paramAsDouble(){

        Generator.addCode("param.asDouble)");

    }

    //% block="将数据转换为 string"  blockType="reporter"
    export function paramAsStr(){

        Generator.addCode("param.asStr()");

    }

    //% block="设置 V[NUM] 引脚的数值 (数字) 为 [TO]"  blockType="command"
    //% NUM.shadow="number" NUM.defl="0"
    //% TO.shadow="number" TO.defl="1"
    export function SetPinValueInt(parameter:any){
        let pin=parameter.NUM.code;
        let value=parameter.TO.code;

        Generator.addCode(`Blynk.virtualWrite(${pin}, ${value});`);

    }

    //% block="设置 V[NUM] 引脚的数值 (字符串) 为 [TO]"  blockType="command"
    //% NUM.shadow="number" NUM.defl="0"
    //% TO.shadow="string" TO.defl="hello"
    export function SetPinValueString(parameter:any){
        let pin=parameter.NUM.code;
        let value=parameter.TO.code;

        Generator.addCode(`Blynk.virtualWrite(${pin}, ${value});`);

    }

    //% block="---"
    export function noteSep1() {}
    
    /*
    //% block="【暂停】 获取虚拟引脚 V[NUM] 的 int 数据并存储到 [VARTO] 变量"  blockType="command"
    //% NUM.shadow="number" NUM.defl="0"
    //% VARTO.shadow="string" VARTO.defl="i"
    export function getVirutalPinInt(parameter:any){
        let pin=parameter.NUM.code;
        let varTo=parameter.VARTO.code;
        if(varTo.substring(0,1)=="\"")  varTo=varTo.substring(1,varTo.length-1);

        Generator.addCode(`BLYNK_WRITE(V${pin}){ ${varTo} = param.asInt(); }`);    

    }

    //% block="【暂停】 获取虚拟引脚 V[NUM] 的 float 数据并存储到 [VARTO] 变量"  blockType="command"
    //% NUM.shadow="number" NUM.defl="0"
    //% VARTO.shadow="string" VARTO.defl="i"
    export function getVirutalPinFloat(parameter:any){
        let pin=parameter.NUM.code;
        let varTo=parameter.VARTO.code;
        if(varTo.substring(0,1)=="\"")  varTo=varTo.substring(1,varTo.length-1);

        Generator.addCode(`BLYNK_WRITE(V${pin}){ ${varTo} = param.asFloat(); }`);    

    }

    //% block="【暂停】 获取虚拟引脚 V[NUM] 的 double 数据并存储到 [VARTO] 变量"  blockType="command"
    //% NUM.shadow="number" NUM.defl="0"
    //% VARTO.shadow="string" VARTO.defl="i"
    export function getVirutalPinDouble(parameter:any){
        let pin=parameter.NUM.code;
        let varTo=parameter.VARTO.code;
        if(varTo.substring(0,1)=="\"")  varTo=varTo.substring(1,varTo.length-1);

        Generator.addCode(`BLYNK_WRITE(V${pin}){ ${varTo} = param.asDouble(); }`);    

    }

    //% block="【暂停】 获取虚拟引脚 V[NUM] 的 string 数据并存储到 [VARTO] 变量"  blockType="command"
    //% NUM.shadow="number" NUM.defl="0"
    //% VARTO.shadow="string" VARTO.defl="i"
    export function getVirutalPinString(parameter:any){
        let pin=parameter.NUM.code;
        let varTo=parameter.VARTO.code;
        if(varTo.substring(0,1)=="\"")  varTo=varTo.substring(1,varTo.length-1);

        Generator.addCode(`BLYNK_WRITE(V${pin}){ ${varTo} = param.asStr(); }`);    

    }

    //% block="---"
    export function noteSep2() {}
    */

    //% block="LED组件注册 V[NUM] 到标识符 [FLAG]"  blockType="command"
    //% NUM.shadow="number" NUM.defl="0"
    //% FLAG.shadow="string" FLAG.defl="led"
    export function registerBlynkLED(parameter:any){
        let pin=parameter.NUM.code;
        let flag=parameter.FLAG.code;
        if(flag.substring(0,1)=="\"")  flag=flag.substring(1,flag.length-1);

        Generator.addObject(`BlynkLED`, `WidgetLED`, `${flag}(V${pin});`);

    }

    //% block="LED组件 打开 标识符 [FLAG]"  blockType="command"
    //% FLAG.shadow="string" FLAG.defl="led"
    export function openBlynkLED(parameter:any){
        let flag=parameter.FLAG.code;
        if(flag.substring(0,1)=="\"")  flag=flag.substring(1,flag.length-1);

        Generator.addCode(`${flag}.on();`);

    }

    //% block="LED组件 关闭 标识符 [FLAG]"  blockType="command"
    //% FLAG.shadow="string" FLAG.defl="led"
    export function closeBlynkLED(parameter:any){
        let flag=parameter.FLAG.code;
        if(flag.substring(0,1)=="\"")  flag=flag.substring(1,flag.length-1);

        Generator.addCode(`${flag}.off();`);

    }

    //% block="---"
    export function noteSep3() {}
    

    //% block="Terminal组件注册 V[NUM] 到标识符 [FLAG]"  blockType="command"
    //% NUM.shadow="number" NUM.defl="0"
    //% FLAG.shadow="string" FLAG.defl="terminal"
    export function registerBlynkTerminal(parameter:any){
        let pin=parameter.NUM.code;
        let flag=parameter.FLAG.code;
        if(flag.substring(0,1)=="\"")  flag=flag.substring(1,flag.length-1);

        Generator.addObject(`BlynkTerminal`, `WidgetTerminal`, `${flag}(V${pin});`);

    }

    //% block="Terminal组件 换行输出 [STR] 标识符 [FLAG]"  blockType="command"
    //% STR.shadow="string" STR.defl="Hello,Blynk"
    //% FLAG.shadow="string" FLAG.defl="terminal"
    export function printlnBlynkTerminal(parameter:any){
        let str=parameter.STR.code;
        let flag=parameter.FLAG.code;
        if(flag.substring(0,1)=="\"")  flag=flag.substring(1,flag.length-1);

        Generator.addCode(`${flag}.println(${str});`);

    }

    //% block="Terminal组件 不换行输出 [STR] 标识符 [FLAG]"  blockType="command"
    //% STR.shadow="string" STR.defl="Hello,Blynk"
    //% FLAG.shadow="string" FLAG.defl="terminal"
    export function printBlynkTerminal(parameter:any){
        let str=parameter.STR.code;
        let flag=parameter.FLAG.code;
        if(flag.substring(0,1)=="\"")  flag=flag.substring(1,flag.length-1);

        Generator.addCode(`${flag}.print(${str});`);

    }

    //% block="Terminal组件 写入缓冲区 [STR] 标识符 [FLAG]"  blockType="command"
    //% STR.shadow="string" STR.defl="Hello,Blynk"
    //% FLAG.shadow="string" FLAG.defl="terminal"
    export function writeBlynkTerminal(parameter:any){
        let str=parameter.STR.code;
        let flag=parameter.FLAG.code;
        if(flag.substring(0,1)=="\"")  flag=flag.substring(1,flag.length-1);

        Generator.addCode(`${flag}.write(${str});`);

    }

    //% block="Terminal组件 确保所有消息都已发出 标识符 [FLAG]"  blockType="command"
    //% FLAG.shadow="string" FLAG.defl="terminal"
    export function flushBlynkTerminal(parameter:any){
        let flag=parameter.FLAG.code;
        if(flag.substring(0,1)=="\"")  flag=flag.substring(1,flag.length-1);

        Generator.addCode(`${flag}.flush();`);

    }

    //% block="Terminal组件 清屏 标识符 [FLAG]"  blockType="command"
    //% FLAG.shadow="string" FLAG.defl="terminal"
    export function clearBlynkTerminal(parameter:any){
        let flag=parameter.FLAG.code;
        if(flag.substring(0,1)=="\"")  flag=flag.substring(1,flag.length-1);

        Generator.addCode(`${flag}.clear();`);

    }

    //% block="---"
    export function noteSep4() {}

    //% block="通知组件 发送通知 [STR]"  blockType="command"
    //% STR.shadow="string" STR.defl="Hello,Phone!"
    export function sendBlynkNotify(parameter:any){
        let str=parameter.STR.code;

        Generator.addCode(`Blynk.notify(${str});`);

    }

    //% block="设备名称"  blockType="reporter"
    export function getBlynkService(){

        Generator.addCode(`"{DEVICE_NAME}"`);

    }

    //% block="---"
    export function noteSep5() {}

    //% block="RTC时间组件注册 到标识符 [FLAG]"  blockType="command"
    //% FLAG.shadow="string" FLAG.defl="rtc"
    export function registerBlynkRTC(parameter:any){
        let flag=parameter.FLAG.code;
        if(flag.substring(0,1)=="\"")  flag=flag.substring(1,flag.length-1);

        Generator.addObject(`BlynkRTC`, `WidgetRTC`, `${flag};`, true);
        Generator.addSetup(`rtcBrgin`,`rtc.begin();`, true);

    }

    //% block="RTC组件 年"  blockType="reporter"
    export function yearBlynkRTC(){

        Generator.addCode(`year()`);

    }

    //% block="RTC组件 月"  blockType="reporter"
    export function monthBlynkRTC(){

        Generator.addCode(`month()`);

    }

    //% block="RTC组件 天"  blockType="reporter"
    export function dayBlynkRTC(){

        Generator.addCode(`day()`);

    }

    //% block="RTC组件 时"  blockType="reporter"
    export function hourBlynkRTC(){

        Generator.addCode(`hour()`);

    }

    //% block="RTC组件 分"  blockType="reporter"
    export function minuteBlynkRTC(){

        Generator.addCode(`minute()`);

    }

    //% block="RTC组件 秒"  blockType="reporter"
    export function secondBlynkRTC(){

        Generator.addCode(`second()`);

    }

    //% block="---"
    export function noteSepEnd() {}

    //% block="..."  blockType="command"
    export function More(){

    }
}
